<?php

namespace app\services;

use plugin\admin\app\model\Area;
use plugin\admin\app\model\UserAddress;
use support\Request;

class AddressService
{
    public static function add($data)
    {
        if ($data['is_default'] == 1) {
            UserAddress::where('user_id', get_user_id())->update(['is_default' => 0]);
        }

        $areas = Area::whereIn('id', [$data['province'], $data['city'], $data['district']])->get()->toArray();
        $areas = array_combine(
            array_column($areas, 'id'),
            array_values($areas)
        );

        return UserAddress::insertGetId([
            'user_id' => get_user_id(),
            'province' => $areas[$data['province']]['area_name'] ?? '',
            'city' => $areas[$data['city']]['area_name'] ?? '',
            'district' => $areas[$data['district']]['area_name'] ?? '',
            'province_id' => $data['province'],
            'city_id' => $data['city'],
            'district_id' => $data['district'],
            'address' => $data['address'],
            'contact_name' => $data['contact_name'],
            'contact_mobile' => $data['contact_mobile'],
            'is_default' => $data['is_default'],
        ]);

    }

    public static function show($id)
    {
        return UserAddress::where('user_id', get_user_id())->where('id', $id)->first();
    }

    public static function hasOne($userId = null)
    {
        $userId = is_null($userId) ? get_user_id() : $userId;
        return UserAddress::where('user_id', $userId)->first();
    }

    public static function list()
    {
        $addresses = UserAddress::where('user_id', get_user_id())->get()->toArray();

        foreach ($addresses as $key=>$address) {
            $addresses[$key]['format_contact_mobile'] = format_mobile($address['contact_mobile']);
        }

        return $addresses;
    }

    public static function detail($id)
    {
        $address = UserAddress::where('id', $id)->where('user_id', get_user_id())->first();

        if (!$address) {
            throw new \Exception('不存在的地址');
        }

        return $address;
    }

    public static function edit($id, $data)
    {
        $addressInfo = UserAddress::find($id);

        if (!$addressInfo) {
            throw new \Exception('不存在的地址信息');
        }

        if ($data['is_default'] == 1) {
            UserAddress::where('user_id', get_user_id())->update(['is_default' => 0]);
        }

        $areas = Area::whereIn('id', [$data['province'], $data['city'], $data['district']])->get()->toArray();
        $areas = array_combine(
            array_column($areas, 'id'),
            array_values($areas)
        );

        $updateData = [
            'user_id' => get_user_id(),
            'province' => $areas[$data['province']]['area_name'] ?? '',
            'city' => $areas[$data['city']]['area_name'] ?? '',
            'district' => $areas[$data['district']]['area_name'] ?? '',
            'province_id' => $data['province'],
            'city_id' => $data['city'],
            'district_id' => $data['district'],
            'address' => $data['address'],
            'contact_name' => $data['contact_name'],
            'is_default' => $data['is_default'],
        ];

        if (format_mobile($addressInfo['contact_mobile']) != $data['contact_mobile']) {
            $updateData['contact_mobile'] = $data['contact_mobile'];
        }

        UserAddress::where('id', $id)->where('user_id', get_user_id())->update($updateData);
    }

    public static function setDefault($id)
    {
        UserAddress::where('user_id', get_user_id())->update(['is_default' => 0]);
        UserAddress::where('user_id', get_user_id())->where('id', $id)->update(['is_default' => 1]);
    }

    public static function getDefault()
    {
        return UserAddress::where('user_id', get_user_id())->where('is_default', 1)->first();
    }

    public static function delete($id)
    {
        UserAddress::where('id', $id)->where('user_id', get_user_id())->delete();
    }
}